<?php

class ShareAction extends HomeCommonAction {

    public function showMemberShareAjax() {
        $condition = array();
        $condition['userid'] = Session::getLocal('id');
        if ($condition['userid'] <= 0) {
            echo 0;
            return;
        }
        $Share = D('Share');
        import("ORG.Util.AjaxPage"); // 导入分页类
        import("ORG.Util.String");
        $count = $Share->where($condition)->count(); // 查询满足要求的总记录数

        $Page = new AjaxPage($count, 6, 'searchMyShareByPage'); // 实例化分页类传入总记录数和每页显示的记录数
        $show = $Page->show(); // 分页显示输出
        $list = $Share->where($condition)->order('time_add desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();

        for ($i = 0; $i < count($list); $i++) {
            $list[$i]['content'] = String::msubstr(strip_tags($list[$i]['content']), $start = 0, 100);
        }
        $this->assign('id', $_REQUEST['id']);
        $this->assign('page', $show);
        $this->assign('list', $list);
        $this->display('myshare');
    }

    public function searchMemberShareAjax(){
        $condition = array();
        $condition['userid'] = Session::getLocal('id');
        $sort = 'desc';
        if (!empty($_REQUEST['sort'])) {
            $sort = $_REQUEST['sort'];
        }
        $brand = $_REQUEST['brand_word'];
//        echo  $brand = iconv('GB2312', 'UTF-8', $brand);
        if (!empty($brand)) {
            $con = array();
            $con['name'] = $brand;
            $BaseBrand = D('BaseBrand');
            $brand = $BaseBrand->where($con)->find();

            $condition['brand_id'] = $brand['id'];
        }
        $category = $_REQUEST['category_word'];
//        $category = iconv('GB2312', 'UTF-8', $category);
        if (!empty($category)) {
            $con = array();
            $con['name'] = $category;

            $BaseCategory = D('BaseCategory');
            $category = $BaseCategory->where($con)->find();
//            dump($BaseCategory->getlastsql());
            $condition['category_id'] = $category['id'];
        }
        $commodity = $_REQUEST['commodity_word'];
//        $commodity = iconv('GB2312', 'UTF-8', $commodity);
        if (!empty($commodity)) {
            $con = array();
            $con['name'] = $commodity;
            if ($brand['id']) {
                $con['brand_id'] = $brand['id'];
            }
            if ($category['id']) {
                $con['category_id'] = $category['id'];
            }

            $BaseCommodity = D('BaseCommodity');
            $commodity = $BaseCommodity->where($con)->find();
            $condition['commodity_id'] = $commodity['id'];
        }

        $Share = D('Share');
        import("ORG.Util.AjaxPage"); // 导入分页类
        import("ORG.Util.String");
        $count = $Share->where($condition)->count(); // 查询满足要求的总记录数
//        echo $Share->getLastSql();
//        dump($condition);
        $Page = new AjaxPage($count, 6, 'searchMyShareByPage'); // 实例化分页类传入总记录数和每页显示的记录数
        $show = $Page->show(); // 分页显示输出

        $list = $Share->where($condition)->order('time_add ' . $sort)->limit($Page->firstRow . ',' . $Page->listRows)->select();

        for ($i = 0; $i < count($list); $i++) {
            $list[$i]['content'] = String::msubstr(strip_tags($list[$i]['content']), $start = 0, 100);
        }
        $this->assign('id', empty($_REQUEST['question_id']) ? $_POST['question_id'] : $_REQUEST['question_id']);
        $this->assign('page', $show);
        $this->assign('list', $list);
//        dump($list);
        $this->display('search_my_share');
    }

    public function shareList() {
        import("ORG.Util.String");
        $condition = array();
        if (!empty($_REQUEST['commodity_id'])) {
            $condition['commodity_id'] = $_REQUEST['commodity_id'];
        }
        if (!empty($_REQUEST['query'])) {
//             $_REQUEST['query'] = iconv('GB2312', 'UTF-8',$_REQUEST['query'] );
            $condition['caption '] = array('like', "%" . $_REQUEST['query'] . "%");
        }


        $Share = D('Share');
        import("ORG.Util.Page"); // 导入分页类
        $count = $Share->relation('user')->where($condition)->count(); // 查询满足要求的总记录数

        $Page = new Page($count, 8); // 实例化分页类传入总记录数和每页显示的记录数

        $show = $Page->show(); // 分页显示输出

        $list = $Share->relation('user')->where($condition)->order('time_add desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        for ($i = 0; $i < count($list); $i++) {
            $list[$i]['content'] =  "　　".msubstr($list[$i]['content'], 0, 153)."..."; //--codetaste
        }
        $this->assign('list', $list);
        $this->assign('query', $_REQUEST['query']);
//        print_r($list);
        $this->assign('page', $show);
        $this->display('share_list');
    }

    public function show() {
        $id = $_REQUEST['id'];
        if (intval($id) <= 0) {
            return false;
        }
        $Share = D('Share');
        $share_info = $Share->where("id = '{$id}'")->find();
        if (count($share_info) <= 0) {
            return false;
        }
        //获取上一条数据
        $share_shang = $Share->where("id > '{$id}'")->order('id')->find();
        if (count($share_shang) <= 0) {
            $this->assign('shang','');
        }else{
        	$this->assign('shang','上一篇:<a href="__APP__/Home/Share/show?id='.$share_shang['id'].'">'.mb_substr($share_shang['caption'],0,10,'utf-8').'...</a>&nbsp;&nbsp;&nbsp;&nbsp;');
        }
        //获取下一条数据
        $share_xia = $Share->where("id < '{$id}'")->order('id desc')->find();
        if (count($share_xia) <= 0) {
            $this->assign('xia','');
        }else{
        	$this->assign('xia','下一篇:<a href="__APP__/Home/Share/show?id='.$share_xia['id'].'">'.mb_substr($share_xia['caption'],0,10,'utf-8').'...</a>');
        }

        $Share->setInc('count_view', "id = '{$id}'", 1);
        //      品牌印象
        $Brand = D('brand');
        $brand_list = $Brand->where("share_id = '{$id}' and caption is not null ")->order('time_add desc')->limit('0,20')->select();
        $brand_list_more = $Brand->where("share_id = '{$id}'")->order('time_add desc')->limit('20,50')->select();

        //      印象词发表人数
        $brand_member_count = $Brand->where("share_id = '{$id}'")->order('time_add desc')->limit()->group('userid')->select();
        //        echo count($brand_member_count);
        //      发布会员信息
        $User = D('User');
        $user_info = $User->where("id = '{$share_info['userid']}'")->find();
        //      关注该会员人数
        $UserFlow = D('UserFlow');
        // $flow_count = $UserFlow->where("userid = '{$share_info['userid']}'")->count();
        $flow_count = $UserFlow->where("flow_userid = '{$share_info['userid']}'")->count();

        $user_info['flow_count'] = $flow_count;


        $ShareAdded = D('ShareAdded');
        $share_add_list = $ShareAdded->where('share_id  = ' . $id)->select();

        //--codetaste
        $this->setLishiCommodity($share_info['commodity_id']);//-codetaste

        $this->assign('share_added_list', $share_add_list);
        $this->assign('brand_member_count', count($brand_member_count));
        $this->assign('brand_list', $brand_list);
        $this->assign('brand_list_more', $brand_list_more);
        $this->assign('share', $share_info);
        $this->assign('user', $user_info);
        $this->display('share');
    }

    public function addScore() {
        $id = $_REQUEST['id'];
        $score = $_REQUEST['score'];
        if (intval($id) <= 0) {
            return false;
        }
        $Share = D('Share');
        $share_info = $Share->where("id = '{$id}'")->find();
        if (count($share_info) <= 0) {
            return false;
        }

        $UserHistory = D('UserHistory');
        $data = array();
        $data['userid'] = Session::getLocal('id');
        $data['brand_id'] = $share_info['brand_id'];
        $data['category_id'] = $share_info['category_id'];
        $data['commodity_id'] = $share_info['commodity_id'];
        $data['share_id'] = $id;
        $data['score'] = intval($score);
        $data['add_time'] = time();
        $rs = $UserHistory->data($data)->add();

        if ($rs) {

            $score_count = $UserHistory->query('SELECT count( * ) as count , score FROM `cm_user_history`WHERE share_id =' . $id . ' GROUP BY score');

            $count = $UserHistory->where('share_id =' . $id)->count();
            if (count($score_count) > 0) {
                $share_score = 0;
                foreach ($score_count as $one) {
                    $share_score += ( $one['count'] / $count) * $one['score'];
                }
            }
            $rs = $Share->where("id = '{$share_info['id']}'")->save(array('score' => $share_score));


            $count = $UserHistory->where('commodity_id =' . $share_info['commodity_id'])->count();

            $share_user_count = $UserHistory->query('SELECT count(*) as count , share_id FROM `cm_user_history` WHERE commodity_id = ' . $share_info['commodity_id'] . ' GROUP BY share_id');
//           print_r($share_user_count);
            $share_list = $Share->where($condition)->select();
            $commodity_score = 0;
            foreach ($share_user_count as $user) {
                $score = 0;
                foreach ($share_list as $share) {
                    if ($share['id'] == $user['share_id']) {
                        $score = $share['score'];
                        break;
                    }
                }
                $commodity_score += ( $user['count'] / $count) * $score;
            }

            $BaseCommodity = D('BaseCommodity');
            $rs = $BaseCommodity->where("id = '{$share_info['commodity_id']}'")->save(array('score' => $commodity_score));

            $uid = Session::getLocal('id');
            $commodityAvg = $UserHistory->where('userid = ' . $uid . ' and commodity_id =' . $share_info['commodity_id'])->avg('score');


            $UserCommodityScore = D('UserCommodityScore');
            $UserCommodityScore->data(array('user_id' => $uid, "commodity_id" => $share_info['commodity_id'], "brand_id" => $share_info['brand_id'], "category_id" => $share_info['category_id']))->add();
            $rs = $UserCommodityScore->where('user_id = ' . $uid . ' and commodity_id =' . $share_info['commodity_id'])->save(array('commodity_score' => $commodityAvg));

            echo 1;
//            $this->display('debug');
        } else {
            echo 0;
        }
    }

    public function showAdd() {
        $this->checkLogin();
        $hangye = getIndustry();
        $this->assign('hangye', $hangye);
        $this->display('personfa_share');
    }

    public function add() {
        $this->checkLogin();
        $brand = trim($_POST['brand']);
        $category = trim($_POST['category']);
        $commodity = trim($_POST['commodity']);
        $industry = trim($_POST['industry']);
        $caption = trim($_POST['title']);
        $content = trim($_POST['content']);



        if (empty($brand) || empty($category) || empty($commodity) || empty($caption) || empty($content) || empty($industry)) {
            return false;
        }
        $BaseBrand = D('BaseBrand');
        $base_brand = $BaseBrand->where("name = '{$brand}'")->find();
        $BaseBrand->startTrans();
        $falg = true;
        if (count($base_brand) > 0) {
            $brand_id = $base_brand['id'];
            if (!$BaseBrand->setInc('add_num', "id = '{$base_brand['id']}'", 1)) {
                $falg = false;
            }
        } else {
            $data = array();
            $data['name'] = $brand;
            $data['industry_id'] = $industry;
            $data['add_time'] = time();
            $brand_id = $BaseBrand->data($data)->add();
            if (!$brand_id) {
                $falg = false;
            }
        }
        $BaseCategory = D('BaseCategory');
        $base_category = $BaseCategory->where("name = '{$category}'")->find();
        if (count($base_category) > 0) {
            $category_id = $base_category['id'];
            if (!$BaseCategory->setInc('add_num', "id = '{$base_category['id']}'", 1)) {
                $falg = false;
            }
        } else {
            $data = array();
            $data['name'] = $category;
            $data['add_time'] = time();
            $data['industry_id'] = $industry;
            $category_id = $BaseCategory->data($data)->add();
            if (!$category_id) {
                $falg = false;
            }
        }

        $BaseCommodity = D('BaseCommodity');
        $base_commodity = $BaseCommodity->where("name = '{$commodity}' and brand_id = '{$base_brand['id']}' and category_id = '{$base_category['id']}'")->find();
        if (count($base_commodity) > 0) {
            $commodity_id = $base_commodity['id'];
            if (!$BaseCommodity->setInc('add_num', "id = '{$base_commodity['id']}'", 1)) {
                $falg = false;
            }
        } else {
            $data = array();
            $data['name'] = htmlspecialchars($commodity);
            $data['add_time'] = time();
            $data['brand_id'] = $brand_id;
            $data['category_id'] = $category_id;
            $data['industry_id'] = $industry;
            $commodity_id = $BaseCommodity->data($data)->add();
            if (!$commodity_id) {
                $falg = false;
            }
        }

        $Share = D('Share');
        $data = array();
        $data['caption'] = htmlspecialchars($caption);
        $data['content'] = htmlspecialchars($content);
        $data['brand_id'] = $brand_id;
        $data['category_id'] = $category_id;
        $data['commodity_id'] = $commodity_id;
        $data['industry_id'] = $industry;
        $data['time_add'] = time();
        $data['time_update'] = time();
        $data['userid'] = Session::getLocal('id');

        $share_id = $Share->data($data)->add();

        if (!$share_id) {
            $falg = false;
        }

        //    $this->display('debug');
        if ($falg) {
            $BaseBrand->commit();
            $User = D("User");
            $User->setInc("score", "id =" . $data['userid'], 1);
            redirect(__APP__ . "/Home/Share/show/id/{$share_id}");
        } else {
            $BaseBrand->rollback();
            //  redirect(__APP__."/Home/");
            return false;
        }
    }

    public function autoBrand() {
        $condition = trim($_REQUEST['q']);
        $BaseBrand = D('BaseBrand');
        $data = array();
        if (empty($condition)) {
            $data = $BaseBrand->limit(10)->order('add_time desc')->select();
        } else {
            $data = $BaseBrand->where("(name like '%{$condition}%' or py like '%{$condition}%')")->limit(10)->order('add_time')->select();
        }
        $out_data = array();
        if (count($data) > 0) {
            foreach ($data as $one) {
                $out_data[] = $one['py'] . '-' . $one['name'];
            }
        }

        echo json_encode($out_data);
    }

    public function autoCategory() {
        $condition = trim($_REQUEST['b']);

        $condition = trim($_REQUEST['q']);
        $BaseCategory = D('BaseCategory');
        $data = array();
        if (empty($condition)) {
            $data = $BaseCategory->where($sql1)->limit(10)->order('add_time desc')->select();
        } else {
            $data = $BaseCategory->where($sql . "(name like '%{$condition}%' or py like '%{$condition}%')")->limit(10)->order('add_time')->select();
        }
        //        print_r($data);
        $out_data = array();
        if (count($data) > 0) {
            foreach ($data as $one) {
                $out_data[] = $one['py'] . '-' . $one['name'];
            }
        }
        echo json_encode($out_data);
        //       $this->display('debug');
    }

    public function autoCommodity() {
        $condition = trim($_REQUEST['b']);
        $condition = trim($_REQUEST['q']);
        $BaseCommodity = D('BaseCommodity');
        $data = array();
        if (empty($condition)) {
            $data = $BaseCommodity->where($sql1)->limit(10)->order('add_time desc')->select();
        } else {
            $data = $BaseCommodity->where($sql . "(name like '%{$condition}%' or py like '%{$condition}%')")->limit(10)->order('add_time')->select();
        }
        $out_data = array();
        if (count($data) > 0) {
            foreach ($data as $one) {
                $out_data[] = $one['py'] . '-' . $one['name'];
            }
        }
        echo json_encode($out_data);
    }

    //    关注
    public function addFlow() {
        $this->checkLogin();
        $share_id = $_REQUEST['id'];
        if (intval($share_id) <= 0) {
            echo '0';
            return false;
        }

        $condition = array();
        $condition['id'] = $share_id;
        $Share = D('Share');
        $share_info = $Share->where($condition)->find();

        if (count($share_info) <= 0) {
            echo '0';
            return false;
        }



        $condition = array();
        $condition['userid'] = Session::getLocal('id');

        if ($condition['userid'] == $share_info['userid']) {
            echo '2';
            return false;
        }

        $condition = array();
        $condition['userid'] = Session::getLocal('id');
        $condition['flow_userid'] = $share_info['userid'];
        $UserFlow = D('UserFlow');

        $flow_info = $UserFlow->where($condition)->find();
        if (count($flow_info) > 0) {
            echo '3';
            return true;
        }

        $data = array();
        $data['userid'] = Session::getLocal('id');
        $data['flow_userid'] = $share_info['userid'];
        $data['add_time'] = time();

        if ($UserFlow->data($data)->add()) {
            echo '1';
            return true;
        } else {
            echo '0';
            return true;
        }
    }

    //采纳
    public function addFavorites() {
        $this->checkLogin();
        $share_id = $_REQUEST['id'];
        if (intval($share_id) <= 0) {
            echo '0';
            return false;
        }

        $condition = array();
        $condition['id'] = $share_id;
        $Share = D('Share');
        $share_info = $Share->where($condition)->find();
        //       print_r($share_info);
        if (count($share_info) <= 0) {
            echo '0';
            return false;
        }


        $condition = array();
        $condition['userid'] = Session::getLocal('id');
        $condition['share_id'] = $share_id;

        $UserFavorites = D('UserFavorites');
        $favorites = $UserFavorites->where($condition)->find();
        if (count($favorites) > 0) {
            echo '3';
            return true;
        }

        $data = array();
        $data['userid'] = Session::getLocal('id');
        $data['share_id'] = $share_info['id'];
        $data['brand_id'] = $share_info['brand_id'];
        $data['categroy_id'] = $share_info['category_id'];
        $data['commodity_id'] = $share_info['commodity_id'];
        $data['time_add'] = time();

        if ($UserFavorites->data($data)->add()) {
            $Share->setInc('count_fav', "id = '{$share_info['id']}'", 1);
            echo '1';
            return true;
        } else {
            echo '0';
            return true;
        }
        //        $this->display('debug');
    }

    public function del() {
        $this->checkLogin();
        $Share = D('Share');
        $id = $_REQUEST['id'];
        if (intval($id) <= 0) {
            echo 0;
        } else {
            $rs = $Share->where('id = ' . $id)->save(array('is_del' => '1'));
            if ($rs) {
                echo 1;
            }
        }
        return;
    }

    public function top() {
        $this->checkLogin();
        $Share = D('Share');
        $id = $_REQUEST['id'];
        if (intval($id) <= 0) {
            echo 0;
        } else {
            $time = time();
            $rs = $Share->where('id = ' . $id)->save(array('time_add' => $time));
            if ($rs) {
                echo 1;
            }
        }
        return;
    }

    public function showAdded() {
        $this->checkLogin();
        $id = $_REQUEST['id'];
        if (intval($id) <= 0) {
            echo "<script>window.history.go(-1);</script>";
            return false;
        }

        $Share = D('Share');
        $share = $Share->where("id = '{$id}'")->find();
        if (count($share) <= 0) {
            echo "<script>window.history.go(-1);</script>";
            return false;
        }

        $this->assign('share', $share);
        $this->assign('id', $id);
        $this->display('show_share_added');
    }

    public function shareAdded() {
        $this->checkLogin();
        $id = $_POST['id'];

        if (intval($id) <= 0) {
            echo "<script>window.history.go(-1);</script>";
            return false;
        }

        $Share = D('Share');
        $share = $Share->where("id = '{$id}'")->find();
        if (count($share) <= 0) {
            echo "<script>window.history.go(-1);</script>";
            return false;
        }

        $data = array();

        $data['share_id'] = $id;
        $data['content'] = htmlspecialchars($_POST['content'], ENT_QUOTES);
        $data['add_time'] = time();

        $ShareAdded = D('ShareAdded');
        $rs = $ShareAdded->add($data);
        if ($rs) {
            redirect(__APP__ . "/Home/Share/show/id/{$id}");
            return;
        }
        echo "<script>window.history.go(-1);</script>";
        return false;
    }

    public function uploadPic() {

        import("ORG.Net.UploadFile");
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize = 32922222;
        //设置上传文件类型
        $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');
        //设置附件上传目录
        $upload->savePath = './Public/Uploads/';
        // 文件重名自动覆盖
        $upload->uploadReplace = 'true';
        $upload->thumb = true;
        $upload->thumbMaxWidth = 500;
        $upload->thumbMaxHeight = 500;
        // 文件命名方式 time
        $upload->saveRule = 'time';

        if (!$upload->upload()) { // 上传错误提示错误信息
            echo 'success';
        } else { // 上传成功获取上传文件信息
            $info = $upload->getUploadFileInfo();
            echo $info[0]['savename'];
        }
    }

    /**
     * codetaste
     * 浏览历史记录
     * @param unknown_type $id
     */
    public function setLishiCommodity($id){
    	$lishi_commodity_old = $_COOKIE["lishi_commodity"];
    	$lishi_commodity_old = str_replace("\\","",$lishi_commodity_old);
    	$lishi_commodity_arr = unserialize(unserialize($lishi_commodity_old));

    	if(!$lishi_commodity_arr) $lishi_commodity_arr = array();
    	if(count($lishi_commodity_arr) >= 5){
    		array_shift($lishi_commodity_arr);
    	}
    	if(!in_array($id,$lishi_commodity_arr)){
	    	array_push($lishi_commodity_arr, $id);
    	}
    	$lishi_commodity = serialize($lishi_commodity_arr);
    	cookie("lishi_commodity",$lishi_commodity,time()+3600,'/');
    }

    /**
     * codetaste
     * 显示商品列表
     * @param unknown_type $id
     */
    public function spList(){
    	if($_REQUEST['is_user']){
	    	$map_u['userid'] = Session::getLocal('id');
	    	$user_list = $this->getListField(M("Share"), $map_u,"commodity_id",$order='','0, 15','commodity_id');
	    	foreach($user_list as $k=>$v){
	    		$map_user_list[] = $v['commodity_id'];
	    	}
	    	$map_u_user_str = implode(",", $map_user_list);
	    	$map['_string'] = "id in ($map_u_user_str)";
    	}
    	if($_REQUEST['search_key']){
    		if($map['_string']) $map['_string'] .= " and ";
    		$map['_string'] .= " name like '%".$_REQUEST['search_key']."%' or py like '%".$_REQUEST['search_key']."%'";
    	}
    	$mapx = array("is_user"=>$_REQUEST['is_user'], "search_key"=>$_REQUEST['search_key']);
    	$this->assign("_mapx", $mapx);
    	$list = $this->_list(M("BaseCommodity"), $map, "py asc","15",$mapx);
    	$this->display("sp_list");
    }


    public function getCommodityInfo(){
    	if($_REQUEST['id']){
    		$info = $this->getInfo(M("BaseCommodity"),$_REQUEST['id']);
    		$info['category_name'] = getAssignInfo($info['category_id'], "BaseCategory", "name");
    		$info['brand_name'] = getAssignInfo($info['category_id'], "BaseBrand", "name");

    		die(json_encode($info));
    	}else{
    		$data['error'] = 1;
    		die(json_encode($data));
    	}

    }

    public function adds(){
    	$this->checkLogin();
    	if($_REQUEST['is_newsp'] == 1){
    		$sp_info = $this->createSp();
    	}else{
    		$sp_info = $_REQUEST;
    	}

    	if($_REQUEST['content_type'] == 1){
    		$result = $this->addAttachment($sp_info);
    	}elseif($_REQUEST['content_type'] == 2){
    		$result = $this->addShare($sp_info);
    	}
    	if($result){
    		$this->success("添加成功");
    	}else{
    		$this->error("添加失败");
    	}
    }

    public function createSp(){
    	$sp_info = $_REQUEST;
    	$cx_brand = $this->getInfo(M("BaseBrand"),'',array("name"=>$_REQUEST['brand']));
    	$cx_category = $this->getInfo(M("BaseCategory"),'',array("name"=>$_REQUEST['category']));
    	$cx_commodity = $this->getInfo(M("BaseCommodity"),'',array("name"=>$_REQUEST['commodity']));
    	if($cx_brand['id']){
    		$sp_info['brand_id'] = $cx_brand['id'];
    	}else{
    		$sp_info['brand_id'] = D("Share")->addBrand();
    	}

    	if($cx_category['id']){
    		$sp_info['category_id'] = $cx_category['id'];
    	}else{
    		$sp_info['category_id'] = D("Share")->addCategory();
    	}

    	if($cx_commodity['id']){
    		$sp_info['commodity_id'] = $cx_commodity['id'];
    	}else{
    		$sp_info['commodity_id'] = D("Share")->addCommodity($sp_info);
    	}
    	return $sp_info;
    }

    public function addAttachment($sp_info){
		if($_FILES['pic']['name']){
			$path = date("Ym",time())."/";
			$up_info = $this->uploads($path);
			$data['file'] = $path.$up_info['0']['savename'];
		} //上传图片end
		if(!$data['file']) showMsg("没有正确上传图片");
		$data['commodity_id'] = $sp_info['commodity'];
    	$data['type'] = "img";
		$data['name'] = $sp_info['title'];
		$data['message'] = $sp_info['message'];
		$data['user_id'] = Session::getLocal('id');
		$data['create_time'] = time();
		$data['status'] = 1;
		$result = M("Attachment")->data($data)->add();
		return $result;
    }

    public function addShare($sp_info){
    	$data['brand_id'] = $sp_info['brand_id'];
    	$data['category_id'] = $sp_info['category_id'];
    	$data['commodity_id'] = $sp_info['commodity_id'];
    	$data['industry_id'] = $sp_info['industry_id'];
    	$data['caption'] = $sp_info['title'];
    	$data['content'] = $sp_info['content'];
    	$data['user_id'] = Session::getLocal('id');
		$data['time_add'] = time();
		$data['time_update'] = time();
		$result = M("Share")->data($data)->add();
    	return $result;
    }













}
?>

